package com.sgu6.netty.utils;

import com.google.protobuf.Any;
import com.sgu6.netty.container.ChannelContainer;
import com.sgu6.netty.enums.CommandPriorityEnum;
import com.sgu6.netty.protobuffer.NettyMessageProto;
import com.sgu6.netty.protobuffer.PacketBuilder;

public class NettyHelper {

    private static PacketBuilder pb = new PacketBuilder();

    public static void postMsgWithCommandResp(String machineNo, String msg) {
        NettyMessageProto.NettyMessage.Builder builder = NettyMessageProto.NettyMessage.newBuilder();
        builder.setHeader(pb.createHeader(1, NettyMessageProto.Header.Type.COMMAND_RESP, null));
        builder.setBody(Any.pack(pb.createMsg(machineNo, msg, true)));
        ChannelContainer.CHANNEL.writeAndFlush(builder.build());
    }

    public static void postMsgWithErrorReq(String machineNo, int errorCode, String content) throws Exception {
        NettyMessageProto.NettyMessage.Builder builder = NettyMessageProto.NettyMessage.newBuilder();
        builder.setHeader(pb.createHeader(1, NettyMessageProto.Header.Type.ERROR_REQ, null));
        builder.setBody(Any.pack(pb.createError(machineNo, errorCode, content)));
        ChannelContainer.CHANNEL.writeAndFlush(builder.build());
    }

    public static void postMsgWithHeartbeatReq(String machineNo, String machineVersion) {
        NettyMessageProto.NettyMessage.Builder builder = NettyMessageProto.NettyMessage.newBuilder();

        builder.setHeader(pb.createHeader(CommandPriorityEnum.普通级.code(), NettyMessageProto.Header.Type.HEARTBEAT_REQ, null));
        builder.setBody(Any.pack(pb.createMsg(machineNo, machineVersion, true)));
        ChannelContainer.CHANNEL.writeAndFlush(builder.build());
    }

    public static void postMsgWithLoginReq(String machineNo, String nonceStr, long timestamp, String signature) {
        //创建回复信息
        NettyMessageProto.NettyMessage.Builder builder = NettyMessageProto.NettyMessage.newBuilder();

        builder.setHeader(pb.createHeader(CommandPriorityEnum.业务级.code(), NettyMessageProto.Header.Type.LOGIN_REQ, null));
        builder.setBody(Any.pack(pb.createLogin(machineNo, nonceStr, timestamp, signature)));
        ChannelContainer.CHANNEL.writeAndFlush(builder.build());
    }

    public static void postMsgWithFinishOrderReq(String barnId, String machineNo, String orderNo, int finishNo, int makeNo) {

        //向服务器发送出米结果
        NettyMessageProto.NettyMessage.Builder builder = NettyMessageProto.NettyMessage.newBuilder();
        builder.setHeader(pb.createHeader(5, NettyMessageProto.Header.Type.FINISH_ORDER_REQ, null));
        if (barnId != null) {
            builder.setBody(Any.pack(pb.createFinishOrder(barnId, machineNo, orderNo, finishNo, makeNo)));
        } else {
            builder.setBody(Any.pack(pb.createFinishOrder("1", machineNo, orderNo, finishNo, makeNo)));
        }
        ChannelContainer.CHANNEL.writeAndFlush(builder.build());
    }

    public static void postMsgWithOperateResp(String machineNo) {
        //创建回复信息
        NettyMessageProto.NettyMessage.Builder builder = NettyMessageProto.NettyMessage.newBuilder();
        builder.setHeader(pb.createHeader(CommandPriorityEnum.普通级.code(), NettyMessageProto.Header.Type.OPERATE_RESP, null));
        builder.setBody(Any.pack(pb.createMsg(machineNo, "收到服务器的命令，并执行", true)));
        //发送
        ChannelContainer.CHANNEL.writeAndFlush(builder.build());
    }


}
